{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:Deprecation warnings have been disabled. Set TF_ENABLE_DEPRECATION_WARNINGS=1 to re-enable them.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"2\"\n",
    "import tensorflow as tf\n",
    "import keras.backend.tensorflow_backend as KTF\n",
    " \n",
    "config = tf.ConfigProto()  \n",
    "config.gpu_options.allow_growth=True  \n",
    "session = tf.Session(config=config)\n",
    " \n",
    "KTF.set_session(session)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from preprocessing import *\n",
    "from model import *\n",
    "from evaluation import *\n",
    "from PrivateKT import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Hyper-Parameter Settings\n",
    "\n",
    "data_path = '../demo_data/'\n",
    "\n",
    "public_data_ratio = 0.2\n",
    "alpha = 0.5\n",
    "\n",
    "epsilon = 5\n",
    "K = 2\n",
    "B = 25\n",
    "\n",
    "lr = 0.05\n",
    "T = 500"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load Data\n",
    "train_images, train_labels = load_data(data_path,'train')\n",
    "test_images, test_labels = load_data(data_path,'test')\n",
    "train_images,train_labels,public_images = partition_public_dataset(train_images, train_labels,public_data_ratio)\n",
    "train_users = local_data_partition(train_labels,alpha=alpha)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# PrivateKT Setting\n",
    "beta = calculate_beta(epsilon,K,train_labels.shape[1])\n",
    "knowledge_buffer = KnowledgeBuffer(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize model\n",
    "model = get_model(lr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 0.1009\n",
      "1 0.1135\n",
      "2 0.1429\n",
      "3 0.2075\n",
      "4 0.2072\n",
      "5 0.2033\n",
      "6 0.2065\n",
      "7 0.2122\n",
      "8 0.2238\n",
      "9 0.2979\n",
      "10 0.3196\n",
      "11 0.3505\n",
      "12 0.3794\n",
      "13 0.3699\n",
      "14 0.3797\n",
      "15 0.4017\n",
      "16 0.4347\n",
      "17 0.436\n",
      "18 0.4513\n",
      "19 0.4734\n",
      "20 0.4947\n",
      "21 0.5193\n",
      "22 0.5337\n",
      "23 0.5416\n",
      "24 0.5694\n",
      "25 0.5771\n",
      "26 0.5729\n",
      "27 0.5906\n",
      "28 0.6059\n",
      "29 0.6097\n",
      "30 0.6096\n",
      "31 0.6414\n",
      "32 0.6566\n",
      "33 0.668\n",
      "34 0.7\n",
      "35 0.6903\n",
      "36 0.7157\n",
      "37 0.7\n",
      "38 0.7126\n",
      "39 0.7142\n",
      "40 0.7199\n",
      "41 0.7164\n",
      "42 0.7163\n",
      "43 0.7318\n",
      "44 0.7218\n",
      "45 0.7325\n",
      "46 0.7404\n",
      "47 0.7444\n",
      "48 0.749\n",
      "49 0.7609\n",
      "50 0.7814\n",
      "51 0.7782\n",
      "52 0.7826\n",
      "53 0.795\n",
      "54 0.797\n",
      "55 0.7884\n",
      "56 0.7972\n",
      "57 0.7972\n",
      "58 0.7922\n",
      "59 0.796\n",
      "60 0.7986\n",
      "61 0.7923\n",
      "62 0.8068\n",
      "63 0.812\n",
      "64 0.8175\n",
      "65 0.8156\n",
      "66 0.8193\n",
      "67 0.8199\n",
      "68 0.8329\n",
      "69 0.8123\n",
      "70 0.8377\n",
      "71 0.8371\n",
      "72 0.8414\n",
      "73 0.8433\n",
      "74 0.8417\n",
      "75 0.8465\n",
      "76 0.8485\n",
      "77 0.8566\n",
      "78 0.843\n",
      "79 0.8574\n",
      "80 0.8692\n",
      "81 0.8736\n",
      "82 0.8708\n",
      "83 0.8753\n",
      "84 0.8772\n",
      "85 0.8761\n",
      "86 0.8803\n",
      "87 0.8792\n",
      "88 0.8854\n",
      "89 0.8834\n",
      "90 0.8841\n",
      "91 0.8842\n",
      "92 0.8833\n",
      "93 0.8879\n",
      "94 0.8952\n",
      "95 0.8859\n",
      "96 0.8911\n",
      "97 0.8912\n",
      "98 0.8922\n",
      "99 0.8935\n",
      "100 0.8923\n",
      "101 0.893\n",
      "102 0.8935\n",
      "103 0.8994\n",
      "104 0.8917\n",
      "105 0.905\n",
      "106 0.9051\n",
      "107 0.9062\n",
      "108 0.9059\n",
      "109 0.9065\n",
      "110 0.9077\n",
      "111 0.9062\n",
      "112 0.9008\n",
      "113 0.9044\n",
      "114 0.9014\n",
      "115 0.9069\n",
      "116 0.9112\n",
      "117 0.9138\n",
      "118 0.9117\n",
      "119 0.9089\n",
      "120 0.9082\n",
      "121 0.9144\n",
      "122 0.911\n",
      "123 0.9153\n",
      "124 0.911\n",
      "125 0.9193\n",
      "126 0.9165\n",
      "127 0.9154\n",
      "128 0.9184\n",
      "129 0.9101\n",
      "130 0.9082\n",
      "131 0.9127\n",
      "132 0.9112\n",
      "133 0.9155\n",
      "134 0.9152\n",
      "135 0.9146\n",
      "136 0.9158\n",
      "137 0.9113\n",
      "138 0.9165\n",
      "139 0.9189\n",
      "140 0.9178\n",
      "141 0.9185\n",
      "142 0.9204\n",
      "143 0.9208\n",
      "144 0.9194\n",
      "145 0.9201\n",
      "146 0.9192\n",
      "147 0.9195\n",
      "148 0.9183\n",
      "149 0.9239\n",
      "150 0.9203\n",
      "151 0.9124\n",
      "152 0.9173\n",
      "153 0.9144\n",
      "154 0.9196\n",
      "155 0.9188\n",
      "156 0.9246\n",
      "157 0.9236\n",
      "158 0.9232\n",
      "159 0.9206\n",
      "160 0.9234\n",
      "161 0.9271\n",
      "162 0.9245\n",
      "163 0.9262\n",
      "164 0.9254\n",
      "165 0.9238\n",
      "166 0.9232\n",
      "167 0.9178\n",
      "168 0.9235\n",
      "169 0.9185\n",
      "170 0.9195\n",
      "171 0.9178\n",
      "172 0.9138\n",
      "173 0.9209\n",
      "174 0.9215\n",
      "175 0.9165\n",
      "176 0.9197\n",
      "177 0.9179\n",
      "178 0.9103\n",
      "179 0.908\n",
      "180 0.9227\n",
      "181 0.9227\n",
      "182 0.9261\n",
      "183 0.9298\n",
      "184 0.9283\n",
      "185 0.9252\n",
      "186 0.9289\n",
      "187 0.9265\n",
      "188 0.9273\n",
      "189 0.9246\n",
      "190 0.9261\n",
      "191 0.9214\n",
      "192 0.9252\n",
      "193 0.9299\n",
      "194 0.9291\n",
      "195 0.9286\n",
      "196 0.9303\n",
      "197 0.931\n",
      "198 0.9314\n",
      "199 0.929\n",
      "200 0.9261\n",
      "201 0.9255\n",
      "202 0.9065\n",
      "203 0.9172\n",
      "204 0.9166\n",
      "205 0.9218\n",
      "206 0.9232\n",
      "207 0.9174\n",
      "208 0.9214\n",
      "209 0.9255\n",
      "210 0.9295\n",
      "211 0.9258\n",
      "212 0.9264\n",
      "213 0.9325\n",
      "214 0.9318\n",
      "215 0.9333\n",
      "216 0.9342\n",
      "217 0.934\n",
      "218 0.9334\n",
      "219 0.9338\n",
      "220 0.9343\n",
      "221 0.9316\n",
      "222 0.931\n",
      "223 0.931\n",
      "224 0.9304\n",
      "225 0.9299\n",
      "226 0.9306\n",
      "227 0.9352\n",
      "228 0.9352\n",
      "229 0.9349\n",
      "230 0.9315\n",
      "231 0.9332\n",
      "232 0.9327\n",
      "233 0.936\n",
      "234 0.9358\n",
      "235 0.928\n",
      "236 0.9365\n",
      "237 0.9367\n",
      "238 0.9352\n",
      "239 0.9338\n",
      "240 0.9335\n",
      "241 0.9349\n",
      "242 0.9321\n",
      "243 0.9302\n",
      "244 0.9336\n",
      "245 0.9359\n",
      "246 0.9293\n",
      "247 0.9273\n",
      "248 0.9333\n",
      "249 0.9358\n",
      "250 0.9292\n",
      "251 0.9327\n",
      "252 0.9347\n",
      "253 0.9381\n",
      "254 0.9356\n",
      "255 0.9373\n",
      "256 0.9331\n",
      "257 0.9349\n",
      "258 0.9345\n",
      "259 0.9364\n",
      "260 0.9408\n",
      "261 0.941\n",
      "262 0.9393\n",
      "263 0.9367\n",
      "264 0.9369\n",
      "265 0.9369\n",
      "266 0.9352\n",
      "267 0.9408\n",
      "268 0.9415\n",
      "269 0.9418\n",
      "270 0.9413\n",
      "271 0.9415\n",
      "272 0.9413\n",
      "273 0.941\n",
      "274 0.942\n",
      "275 0.9436\n",
      "276 0.9423\n",
      "277 0.9437\n",
      "278 0.945\n",
      "279 0.9432\n",
      "280 0.9336\n",
      "281 0.9363\n",
      "282 0.9392\n",
      "283 0.9349\n",
      "284 0.9338\n",
      "285 0.94\n",
      "286 0.9415\n",
      "287 0.9417\n",
      "288 0.9424\n",
      "289 0.9441\n",
      "290 0.945\n",
      "291 0.9456\n",
      "292 0.9477\n",
      "293 0.9467\n",
      "294 0.9472\n",
      "295 0.946\n",
      "296 0.9465\n",
      "297 0.9478\n",
      "298 0.9484\n",
      "299 0.9428\n",
      "300 0.9411\n",
      "301 0.9454\n",
      "302 0.9433\n",
      "303 0.9462\n",
      "304 0.944\n",
      "305 0.947\n",
      "306 0.9475\n",
      "307 0.948\n",
      "308 0.9472\n",
      "309 0.9464\n",
      "310 0.9475\n",
      "311 0.9465\n",
      "312 0.9456\n",
      "313 0.9442\n",
      "314 0.9458\n",
      "315 0.9465\n",
      "316 0.9457\n",
      "317 0.9463\n",
      "318 0.9459\n",
      "319 0.9443\n",
      "320 0.9457\n",
      "321 0.9425\n",
      "322 0.9447\n",
      "323 0.947\n",
      "324 0.9444\n",
      "325 0.9472\n",
      "326 0.9454\n",
      "327 0.9427\n",
      "328 0.941\n",
      "329 0.9446\n",
      "330 0.9455\n",
      "331 0.9445\n",
      "332 0.9471\n",
      "333 0.9447\n",
      "334 0.944\n",
      "335 0.9439\n",
      "336 0.9432\n",
      "337 0.945\n",
      "338 0.9457\n",
      "339 0.9468\n",
      "340 0.9468\n",
      "341 0.9469\n",
      "342 0.9475\n",
      "343 0.9463\n",
      "344 0.9466\n",
      "345 0.9468\n",
      "346 0.9449\n",
      "347 0.9458\n",
      "348 0.9434\n",
      "349 0.943\n",
      "350 0.9425\n",
      "351 0.943\n",
      "352 0.9461\n",
      "353 0.9497\n",
      "354 0.9501\n",
      "355 0.9456\n",
      "356 0.9482\n",
      "357 0.9473\n",
      "358 0.9491\n",
      "359 0.9493\n",
      "360 0.9489\n",
      "361 0.9492\n",
      "362 0.9487\n",
      "363 0.9494\n",
      "364 0.949\n",
      "365 0.9509\n",
      "366 0.945\n",
      "367 0.9448\n",
      "368 0.9467\n",
      "369 0.9452\n",
      "370 0.9434\n",
      "371 0.9455\n",
      "372 0.9457\n",
      "373 0.9438\n",
      "374 0.943\n",
      "375 0.9445\n",
      "376 0.9425\n",
      "377 0.9446\n",
      "378 0.9489\n",
      "379 0.9484\n",
      "380 0.9425\n",
      "381 0.9414\n",
      "382 0.9407\n",
      "383 0.9407\n",
      "384 0.9436\n",
      "385 0.9437\n",
      "386 0.9421\n",
      "387 0.9447\n",
      "388 0.9429\n",
      "389 0.9472\n",
      "390 0.9422\n",
      "391 0.9439\n",
      "392 0.9441\n",
      "393 0.9458\n",
      "394 0.945\n",
      "395 0.9433\n",
      "396 0.9404\n",
      "397 0.9417\n",
      "398 0.9421\n",
      "399 0.9432\n",
      "400 0.9444\n",
      "401 0.9404\n",
      "402 0.9472\n",
      "403 0.946\n",
      "404 0.9439\n",
      "405 0.9441\n",
      "406 0.9457\n",
      "407 0.9468\n",
      "408 0.9476\n",
      "409 0.947\n",
      "410 0.9474\n",
      "411 0.9503\n",
      "412 0.9468\n",
      "413 0.9486\n",
      "414 0.9474\n",
      "415 0.9498\n",
      "416 0.949\n",
      "417 0.9475\n",
      "418 0.9493\n",
      "419 0.9476\n",
      "420 0.9482\n",
      "421 0.9478\n",
      "422 0.9475\n",
      "423 0.9501\n",
      "424 0.9508\n",
      "425 0.9518\n",
      "426 0.9508\n",
      "427 0.9521\n",
      "428 0.9518\n",
      "429 0.9503\n",
      "430 0.9508\n",
      "431 0.9496\n",
      "432 0.9496\n",
      "433 0.9475\n",
      "434 0.9491\n",
      "435 0.9521\n",
      "436 0.9517\n",
      "437 0.9517\n",
      "438 0.95\n",
      "439 0.9488\n",
      "440 0.9486\n",
      "441 0.9487\n",
      "442 0.9443\n",
      "443 0.9483\n",
      "444 0.9503\n",
      "445 0.9513\n",
      "446 0.9514\n",
      "447 0.953\n",
      "448 0.9507\n",
      "449 0.9531\n",
      "450 0.9508\n",
      "451 0.9478\n",
      "452 0.9521\n",
      "453 0.9505\n",
      "454 0.9524\n",
      "455 0.9514\n",
      "456 0.9498\n",
      "457 0.9502\n",
      "458 0.9464\n",
      "459 0.9492\n",
      "460 0.9511\n",
      "461 0.9502\n",
      "462 0.9502\n",
      "463 0.9516\n",
      "464 0.9529\n",
      "469 0.9491\n",
      "470 0.9506\n",
      "471 0.9478\n",
      "472 0.9501\n",
      "473 0.9503\n",
      "474 0.9459\n",
      "475 0.9502\n",
      "476 0.9514\n",
      "477 0.9536\n",
      "478 0.9546\n",
      "479 0.954\n",
      "480 0.9534\n",
      "481 0.9516\n",
      "482 0.9524\n",
      "483 0.9534\n",
      "484 0.9542\n",
      "485 0.9531\n",
      "486 0.9485\n",
      "487 0.9529\n",
      "488 0.9534\n",
      "489 0.9511\n",
      "490 0.9524\n",
      "491 0.9537\n",
      "492 0.9542\n",
      "493 0.9545\n",
      "494 0.9536\n",
      "495 0.9545\n",
      "496 0.9552\n",
      "497 0.9527\n",
      "498 0.9545\n",
      "499 0.951\n"
     ]
    }
   ],
   "source": [
    "Res = []\n",
    "for i in range(T):\n",
    "    PrivateKT(knowledge_buffer,model,train_users,train_images,train_labels,public_images,beta,2,)\n",
    "    acc = evaluation(model,test_images,test_labels)\n",
    "    Res.append(acc)\n",
    "    print(i,acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
